package com.zat.mes.andrmes.dofun.system

import android.view.View
import androidx.core.content.ContextCompat
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.contrarywind.view.WheelView
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.google.gson.Gson
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.base.BaseFragment
import com.zat.mes.andrmes.data.LoginResultBean
import com.zat.mes.andrmes.dofun.system.fragment.ClockInReadFragment
import com.zat.mes.andrmes.dofun.system.fragment.ClockInUnReadFragment
import com.zat.mes.andrmes.event.CheckInUnEventMessage
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.utils.DateUtil
import com.zat.mes.andrmes.utils.GlideUtil
import com.zat.mes.andrmes.utils.PreferencesUtil
import kotlinx.android.synthetic.main.activity_clock_in_read.*
import kotlinx.android.synthetic.main.layout_head_clock.*
import kotlinx.android.synthetic.main.layout_title.*
import org.greenrobot.eventbus.EventBus
import java.util.*

/**
 * 考勤确认
 */
class ClockInReadActivity : BaseActivity(), View.OnClickListener {

    private var timePicker: TimePickerView? = null
    lateinit var willFragment: ClockInReadFragment
    lateinit var hasFragment: ClockInUnReadFragment
    var selectedDate = DateUtil.formatDate(Date(), DateUtil.FORMAT_3)
    var showDate: String? = null
    var empId: Long? = null
    override fun init() {
        initView()
        initData();
    }

    /**
     * 初始化数据
     */
    private fun initData() {
        val loginResultBean = Gson().fromJson(
            PreferencesUtil.getString(CompanionType.LOGIN_RESULT),
            LoginResultBean::class.java
        )
        empId = loginResultBean.empid
        tvNameClock.text = loginResultBean.empname
        GlideUtil.setHeader(this, loginResultBean?.img, ivHeadClock)
        showDate =
            DateUtil.formatDate(Date(), getString(R.string.head_date)) + DateUtil.getWeek(Date())
        setDateText()

        initDatePick()

        willFragment = ClockInReadFragment()
        hasFragment = ClockInUnReadFragment()
        vpClockRead.adapter = adapter
        TabLayoutMediator(
            tabClockRead,
            vpClockRead,
            object : TabLayoutMediator.TabConfigurationStrategy {
                override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
                    when (position) {
                        0 -> tab.text = getString(R.string.text_sure_not)
                        1 -> tab.text = getString(R.string.text_sure_has)
                    }
                }
            }).attach()

    }

    private fun initView() {
        setContentView(R.layout.activity_clock_in_read)
        tVtitle.text = getString(R.string.title_clock_read)
        rlReturnTitle.visibility = View.VISIBLE
        rlRight.visibility = View.GONE
        rlReturnTitle.setOnClickListener(this)
        llCalenClock.setOnClickListener(this)

    }

    val adapter = object : FragmentStateAdapter(this) {
        override fun getItemCount() = 2

        override fun createFragment(position: Int): BaseFragment {
            //0 未审核 1 审核
            return when (position) {
                0 -> willFragment
                1 -> hasFragment

                else -> willFragment
            }
        }
    }

    /**
     * 初始化时间选择器
     */
    private fun initDatePick() {
        timePicker = TimePickerBuilder(this, object : OnTimeSelectListener {
            override fun onTimeSelect(date: Date?, v: View?) {
                if (date != null) {
                    selectedDate = DateUtil.formatDate(date, DateUtil.FORMAT_3)
                    showDate =
                        DateUtil.formatDate(date, getString(R.string.head_date)) + DateUtil.getWeek(
                            date
                        )
                    setDateText()
                    EventBus.getDefault().post(CheckInUnEventMessage())
                }
            }

        }).setSubmitColor(ContextCompat.getColor(this, R.color.color_text_nomal))
            .setCancelColor(ContextCompat.getColor(this, R.color.color_text_nomal))
            .setTitleBgColor(ContextCompat.getColor(this, R.color.color_white))
            .setContentTextSize(18)
            .setLabel("", "", "", "", "", "")
            .setDividerType(WheelView.DividerType.WRAP)
            .setDividerColor(ContextCompat.getColor(this, R.color.color_theme))
            .setLineSpacingMultiplier(2.0f)
            .build()
    }

    /**
     * 设置时间
     */
    fun setDateText() {
        tvCanlendarClock.text = showDate
    }


    override fun onClick(v: View?) {
        when (v) {
            rlReturnTitle -> finish()
            llCalenClock -> timePicker?.show()
        }
    }

}
